﻿namespace FFF.Test.DataAccess.Framework
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Diagnostics;
    using Microsoft.EntityFrameworkCore.Metadata;
    using FFF.Test.DataAccess.Framework.Configuration.Dto;
    using FFF.Test.Util.Consts;

    /// <summary>
    /// 修改连接字符串你串
    /// </summary>
    public static class ChangeDatabase
    {
        /// <summary>
        /// 修改值read数据库
        /// </summary>
        /// <param name="context">context实例</param>
        public static void ChangeToRead(this DbContext context)
        {
            var lstSlave = AppConfigConsts.SlaveConStrs;
            var index = new Random().Next(0, lstSlave.Count);
            context.Database.GetDbConnection().ConnectionString = lstSlave[index];
        }

        /// <summary>
        /// 修改为master数据库
        /// </summary>
        /// <param name="context">context实例</param>
        public static void ChangeToWrite(this DbContext context)
        {
            context.Database.GetDbConnection().ConnectionString = AppConfigConsts.ConStr;
        }
    }

    ///// <summary>
    ///// 命令
    ///// </summary>
    //public class DbCommandInterceptor : IObserver<KeyValuePair<string, object>>
    //{
    //    /// <summary>
    //    /// 主库
    //    /// </summary>
    //    private const string _masterConnectionString = "";

    //    /// <summary>
    //    /// 从库
    //    /// </summary>
    //    private const string _slaveConnectionString = "";

    //    /// <summary>
    //    /// 完成
    //    /// </summary>
    //    public void OnCompleted()
    //    {
    //    }

    //    /// <summary>
    //    /// error
    //    /// </summary>
    //    /// <param name="error">error参数</param>
    //    public void OnError(Exception error)
    //    {
    //    }

    //    /// <summary>
    //    /// 执行下一步
    //    /// </summary>
    //    /// <param name="value">对应的值</param>
    //    public void OnNext(KeyValuePair<string, object> value)
    //    {
    //        if (value.Key == RelationalEventId.CommandExecuting.Name)
    //        {
    //            var command = ((CommandEventData)value.Value).Command;
    //            var executeMethod = ((CommandEventData)value.Value).ExecuteMethod;

    //            if (executeMethod == DbCommandMethod.ExecuteNonQuery)
    //            {
    //                this.ResetConnection(command, _masterConnectionString);
    //            }
    //            else if (executeMethod == DbCommandMethod.ExecuteScalar)
    //            {
    //                this.ResetConnection(command, _slaveConnectionString);
    //            }
    //            else if (executeMethod == DbCommandMethod.ExecuteReader)
    //            {
    //                this.ResetConnection(command, _slaveConnectionString);
    //            }
    //        }
    //    }

    //    /// <summary>
    //    /// 充值
    //    /// </summary>
    //    /// <param name="command">command</param>
    //    /// <param name="connectionString">连接字符串</param>
    //    private void ResetConnection(DbCommand command, string connectionString)
    //    {
    //        if (command.Connection.State == ConnectionState.Open)
    //        {
    //            if (command.CommandText.Contains("@@ROWCOUNT"))
    //            {
    //                command.Connection.Close();
    //                command.Connection.ConnectionString = _masterConnectionString;
    //            }
    //        }

    //        if (command.Connection.State == ConnectionState.Closed)
    //        {
    //            command.Connection.Open();
    //        }
    //    }
    //}

    ///// <summary>
    ///// comandlistenter
    ///// </summary>
    //public class CommandListener : IObserver<DiagnosticListener>
    //{
    //    private readonly DbCommandInterceptor _dbCommandInterceptor = new DbCommandInterceptor();

    //    /// <summary>
    //    /// 完成事件
    //    /// </summary>
    //    public void OnCompleted()
    //    {
    //    }

    //    /// <summary>
    //    /// 错误事件
    //    /// </summary>
    //    /// <param name="error">错误信息</param>
    //    public void OnError(Exception error)
    //    {
    //    }

    //    /// <summary>
    //    /// 执行下一个
    //    /// </summary>
    //    /// <param name="listener">listerer</param>
    //    public void OnNext(DiagnosticListener listener)
    //    {
    //        if (listener.Name == DbLoggerCategory.Name)
    //        {
    //            listener.Subscribe(this._dbCommandInterceptor);
    //        }
    //    }
    //}
}
